package com.eascs.web.o2o.mq.test;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;
import com.rabbitmq.client.AMQP.Queue;

public class RabbitProducer {
	private static final String TASK_QUEUE_NAME = "task_queue";

	public static void main(String[] args) {
		try {
			ConnectionFactory factory = new ConnectionFactory();
			factory.setHost("172.29.12.201");
			factory.setPort(5672);
			factory.setUsername("guest");
			factory.setPassword("kaibin");
			Connection connection = factory.newConnection();
			Channel channel = connection.createChannel();
			channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
			
			// 分发信息
			int index = 0;
			while (true) {
				if(index == 4) return;
				String message = "Hello RabbitMQ" + index++;
				channel.basicPublish("", TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
				System.out.println("NewTask send '" + message + "'");
				try {
					TimeUnit.SECONDS.sleep(1);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}

			 //channel.close();
			 //connection.close();
			 //System.out.println("Rabbit produce start ok");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (java.util.concurrent.TimeoutException e) {
			e.printStackTrace();
		}
	}
}
